/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-31 21:16:59
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-31 21:19:05
 * @Description: file content
 */
#include "apue.h"

ssize_t readn(int fd, void *ptr, size_t n)
{
    ssize_t nread;
    size_t nleft = n;

    while (nleft > 0)
    {
        if ((nread = read(fd, ptr, nleft)) < 0)
        {
            if (nleft == n)
            {
                return-1;
            }
            else
            {
                break;
            }
        }
        else if (0 == nread)
        {
            break;
        }

        nleft -= nread;
        ptr   += nread;
    }

    return (n - nleft);
}
